package com.sl.demo;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphO;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {

        System.out.println( "Hello World!" );
        initFlowRules();
        while (true) {
            doSomething1();
        }
    }

    static void doSomething(){
        try(Entry entry = SphU.entry("doSomething")) {
            System.out.println("hello world "+System.currentTimeMillis());
        }catch (BlockException ex){
            //
        }
    }

    static void doSomething1(){
        if (SphO.entry("sb")) {
            try{
                System.out.println("hello world------ "+System.currentTimeMillis());
            }finally {
                SphO.exit();
            }
        }else
            System.out.println("资源被限制了");
    }

    static void initFlowRules(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("sb");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(20);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);



    }
}
